﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(IPromotionLog))]
    partial class PromotionLog:Entity,IPromotionLog
    {
        IOrder _order;
        public IOrder Order
        {
            get
            {
                if (this._order == null)
                {
                    this._order = this.DataContext.Orders
                        .Where(i => i.Id == this.OrderId)
                        .FirstOrDefault();
                }

                return this._order;
            }
            set
            {
                this._order = value;
                if (value != null)
                    this.OrderId = value.Id;
                else
                    this.OrderId = 0;
            }
        }

        IOrderItem _orderItem;
        public IOrderItem OrderItem
        {
            get
            {
                if (this._orderItem == null)
                {
                    this._orderItem = this.DataContext.OrderItems
                        .Where(i => i.Id == this.OrderItemId)
                        .FirstOrDefault();
                }

                return this._orderItem;
            }
            set
            {
                this._orderItem = value;
                if (value != null)
                    this.OrderItemId = value.Id;
                else
                    this.OrderItemId = 0;
            }
        }

        IPromotionRule _promotionRule;
        public IPromotionRule PromotionRule
        {
            get
            {
                if (this._promotionRule == null)
                {
                    this._promotionRule = this.DataContext.PromotionRules
                        .Where(i => i.Id == this.PromotionRuleId)
                        .FirstOrDefault();
                }

                return this._promotionRule;
            }
            set
            {
                this._promotionRule = value;
                if (value != null)
                    this.PromotionRuleId = value.Id;
                else
                    this.PromotionRuleId= 0;
            }
        }

        IProduct _product;
        public IProduct Product
        {
            get
            {
                if (this._product == null)
                {
                    this._product = this.DataContext.Products
                        .Where(i => i.Id == this.ProductId)
                        .FirstOrDefault();
                }

                return this._product;
            }
            set
            {
                this._product = value;
                if (value != null)
                    this.ProductId = value.Id;
                else
                    this.ProductId = 0;
            }
        }
    }
}
